package com.tdt.alarm.controller;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.tdt.alarm.R;
import com.tdt.alarm.controller.fragment.Fragment2;
import com.tdt.alarm.controller.fragment.Fragment4;
import com.tdt.alarm.controller.fragment.Home;

public class MainActivity extends FragmentActivity implements TabListener {

	ViewPager pager;

	private final String tab1Lable = "Home";
	private final String tab2Lable = "Timer";
	private final String tab3Lable = "Reminder";
	private final String tab4Lable = "Setting";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		pager = (ViewPager) findViewById(R.id.pager);
		FragmentManager fm = getSupportFragmentManager();
		pager.setAdapter(new MyAdapter(fm));

		ActionBar actionBar = getActionBar();
		Tab tab1 = actionBar.newTab();
		tab1.setText(tab1Lable);
		tab1.setTabListener(this);
		actionBar.addTab(tab1);

		Tab tab2 = actionBar.newTab();
		tab2.setText(tab2Lable);
		tab2.setTabListener(this);
		actionBar.addTab(tab2);

//		Tab tab3 = actionBar.newTab();
//		tab3.setText(tab3Lable);
//		tab3.setTabListener(this);
//		actionBar.addTab(tab3);

		Tab tab4 = actionBar.newTab();
		tab4.setText(tab4Lable);
		tab4.setTabListener(this);
		actionBar.addTab(tab4);
		
		

		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		pager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				getActionBar().setSelectedNavigationItem(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	class MyAdapter extends FragmentPagerAdapter {

		public MyAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			Fragment fragment = null;
			switch (position) {
			case 0:
				fragment = new Home();
				break;
			case 1:
				fragment = new Fragment2();
				break;
//			case 2:
//				fragment = new Fragment3();
//				break;
			case 2:
				fragment = new Fragment4();
				break;
			}
			return fragment;
		}

		@Override
		public int getCount() {
			return 3;
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction arg1) {
		if (tab.getText().equals(tab1Lable)) {
			pager.setCurrentItem(0);
		} else if (tab.getText().equals(tab2Lable)) {
			pager.setCurrentItem(1);
		} else if (tab.getText().equals(tab3Lable)){
			pager.setCurrentItem(2);
		} else {
			pager.setCurrentItem(3);
		}
	}

	@Override
	public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub

	}

}
